package com.zhanghaijun.createCode.ad;

import com.zhanghaijun.createCode.ad.bean.AdSynchronousBean;
import com.zhanghaijun.createCode.ad.bean.AdUser;
import com.zhanghaijun.createCode.ad.factory.AdConnectionFactory;
import com.zhanghaijun.createCode.ad.service.AdApiService;
import com.zhanghaijun.createCode.mysql.factory.MysqlConnection;
import com.zhanghaijun.createCode.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by zhanghaijun on 15/6/10.
 */
public class TestAd {


    public static String path=Constant.outPutRootPath + "last.xlsx";//excel 路径
    public static  AdApiService adApiService= new AdApiService();


    public static void main(String[] args){
        //importExcelToAdServer();
        //changePassword("");
        //adApiService.changePassword("zhangsan","","12312");
        //PrintUtil.println(adApiService.loginByAd("zhanghaijun", "a123456"));
        //AdConnectionFactory.add("enniu","zhangsan1");
        //getPinYinName();
        //changeMail("@51zhangdan.com.cn");
        //CompareLoginName();
        //getPinYinName2();
        //getPinYinName3();
//        System.out.println(System.getProperty("user.home"));
//        System.out.println(System.getProperty("java.version"));
//        System.out.println(System.getProperty("os.name"));
//        System.out.println(System.getProperty("java.vendor.url"));
//        try{
//            Map<String,Object> params = new HashMap<String,Object>();
//            String newPassword="136758881093";
//            String newQuotedPassword = "\"" + newPassword + "\"";
//            byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
//            params.put("unicodePwd",newUnicodePassword);
//            adApiService.updateUserProperty("zhanghaijun",params);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

//        try{
//            Map<String,Object> params = new HashMap<String,Object>();
//            params.put("userAccountControl","514");
//            adApiService.updateUserProperty("zhanghaijun",params);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

//        AdSynchronousBean adSynchronousBean = new AdSynchronousBean();
//        AdUser adUser = new AdUser();
//        String disPlayName="章测试";
//        String loginName ="zhangceshi2";
//
//        adUser.setStreetAddress("12312312312");
//        adUser.setDisplayName(disPlayName);
//        adUser.setCn(disPlayName);
//        adUser.setEmployeeId(loginName);
//        adUser.setsAMAccountName(loginName);
//        adUser.setSn(loginName);
//        adUser.setLoginName(loginName);
//        adUser.setUid(loginName);
//        adUser.setUserPassword("");
//        adUser.setUserAccountControl("544");
//        adUser.setMail("12312312@qq.com");
//
//        adSynchronousBean.setAdUser(adUser);
//        adSynchronousBean.setBussinessDept("事业部测试");
//        adSynchronousBean.setCompany("公司测试");
//        adSynchronousBean.setOrg("部门测试");
//        JsonResultDto resultDto = adApiService.addAdUser(adSynchronousBean);
//
//        PrintUtil.println(resultDto.isSuccess());




        //PrintUtil.println("success=================================");

        PrintUtil.println(adApiService.loginByAd("zhanghaijun", "8755018"));

        //PrintUtil.println(adApiService.loginByAd("qianchenzhang", "136758881093"));

        //getPinYinName3();

    }

    /**
     *ad同步
     */
    private static void importExcelToAdServer(){
        try{
            File file = new File(path);

            BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            int minRowIx = sheet.getFirstRowNum() + 1;
            int maxRowIx = sheet.getLastRowNum();
            FormulaEvaluator evaluator = workbook.getCreationHelper()
                    .createFormulaEvaluator();
            PrintUtil.println("add start...");
            for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
                Row row = sheet.getRow(rowNum);
                String loginName=getCellValue(evaluator, row, 1);//login name
                if(StringUtil.isEmpty(loginName)){
                   continue;
                }
                String displayName=getCellValue(evaluator, row, 2);
                String company = getCellValue(evaluator, row, 5);//公司
                String division = getCellValue(evaluator,row,6);//事业部
                String orgId = getCellValue(evaluator,row,7);//部门
                String address = getCellValue(evaluator,row,26);//地址
                String mail = getCellValue(evaluator,row,24);//邮箱

                AdSynchronousBean adSynchronousBean = new AdSynchronousBean();
                adSynchronousBean.setCompany(company);
                adSynchronousBean.setBussinessDept(division);
                adSynchronousBean.setOrg(orgId);
                AdUser adUser = new AdUser();
                adUser.setStreetAddress(address);
                adUser.setDisplayName(displayName);
                adUser.setCn(displayName);
                adUser.setEmployeeId(displayName);
                adUser.setsAMAccountName(displayName);
                adUser.setSn(loginName);
                adUser.setLoginName(loginName);
                adUser.setUid(loginName);
                adUser.setUserPassword("");
                adUser.setUserAccountControl("544");
                adUser.setMail(mail);
                adSynchronousBean.setAdUser(adUser);

                JsonResultDto res = adApiService.addAdUser(adSynchronousBean);
                if(res.isSuccess()){
                    PrintUtil.println(adUser.getDisplayName()+res.getData());
                }else{
                    PrintUtil.println(adUser.getDisplayName()+"添加失败");
                }
            }
            PrintUtil.println("add end...");
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     *批量修改密码
     */
    private static void changePassword(String oldPassword){
        try{
            File file = new File(path);
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            int minRowIx = sheet.getFirstRowNum() + 1;
            int maxRowIx = sheet.getLastRowNum();
            FormulaEvaluator evaluator = workbook.getCreationHelper()
                    .createFormulaEvaluator();
            PrintUtil.println("changePassword start...");
            for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
                Row row = sheet.getRow(rowNum);
                String loginName=getCellValue(evaluator, row, 1);//login name
                if(StringUtil.isEmpty(loginName)){
                    continue;
                }
                JsonResultDto res = adApiService.changePassword(loginName, "", "a123456");
                if(res.isSuccess()){
                    PrintUtil.println("loginName:"+loginName+res.getData());
                }else{
                    PrintUtil.println("fail loginName:"+loginName+res.getData());
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *批量修改邮箱
     */
    private static void changeMail(String mailSuffix){
        try{
            File file = new File(path);
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            int minRowIx = sheet.getFirstRowNum() + 1;
            int maxRowIx = sheet.getLastRowNum();
            FormulaEvaluator evaluator = workbook.getCreationHelper()
                    .createFormulaEvaluator();
            PrintUtil.println("updateMail start...");
            for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
                Row row = sheet.getRow(rowNum);
                String loginName=getCellValue(evaluator, row, 1);//login name
                if(StringUtil.isEmpty(loginName)){
                    continue;
                }
                String mail = loginName+mailSuffix;
                JsonResultDto res = adApiService.changeMail(loginName, mail);
                if(res.isSuccess()){
                    //PrintUtil.println("loginName:"+loginName+"邮箱更改成功!"+"邮箱是:"+mail);
                }else{
                    PrintUtil.println("fail loginName:"+loginName+"邮箱更改失败");
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 获取pinyin
     */
    public static Map<String,String> getPinYinName(){
        Map<String,String> map = new HashMap<String,String>();
        try{
            String path = Constant.outPutRootPath + "2.xlsx";
            File file = new File(path);
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            int minRowIx = sheet.getFirstRowNum() + 1;
            int maxRowIx = sheet.getLastRowNum();
            FormulaEvaluator evaluator = workbook.getCreationHelper()
                    .createFormulaEvaluator();
            PrintUtil.println("create pinyinname start...");
            for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
                Row row = sheet.getRow(rowNum);
                String displayName=getCellValue(evaluator, row, 2);//display name
                String loginName=getCellValue(evaluator, row, 1);//login name
                map.put(displayName, loginName);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 获取pinyin
     */
    public static Map<String,String> getPinYinName2(){
        Map<String,String> map = new HashMap<String,String>();
        try{
            String path = Constant.outPutRootPath + "3.xlsx";
            File file = new File(path);
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            int minRowIx = sheet.getFirstRowNum() + 1;
            int maxRowIx = sheet.getLastRowNum();
            FormulaEvaluator evaluator = workbook.getCreationHelper()
                    .createFormulaEvaluator();
            PrintUtil.println("create pinyinname start...");
            for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
                Row row = sheet.getRow(rowNum);
                String displayName=getCellValue(evaluator, row, 2);//display name
                String loginName=getCellValue(evaluator, row, 1);//login name
                PrintUtil.println(displayName + ":" + loginName);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }


    public static Map<String,String> getPinYinName3(){
        Map<String,String> map = new HashMap<String,String>();
        try{
            String path = Constant.outPutRootPath + "last.xlsx";
            File file = new File(path);
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            int minRowIx = sheet.getFirstRowNum() + 1;
            int maxRowIx = sheet.getLastRowNum();
            FormulaEvaluator evaluator = workbook.getCreationHelper()
                    .createFormulaEvaluator();
            PrintUtil.println("create pinyinname start...");
            Map<String,String> dataSource = MysqlConnection.getRes();
            for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
                Row row = sheet.getRow(rowNum);
                String displayName=getCellValue(evaluator, row, 2);//display name
                //String loginName=getCellValue(evaluator, row, 1);//login name

                //PrintUtil.println("displayname"+displayName);
                String loginName = dataSource.get(displayName.trim());
                String rowRes="";

                if(loginName==null){
                     loginName=pinyin4j.getPinyin(displayName.trim()).toString();
                     rowRes ="Names.put(\""+displayName.trim()+"\",null);";
                }else{
                     rowRes ="Names.put(\""+displayName.trim()+"\",\""+loginName+"\");";
                }

                //PrintUtil.println(rowRes);
                PrintUtil.println(loginName);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }



    public static String getCellValue(FormulaEvaluator evaluator,Row row,int colIndex){
        Cell cell = row.getCell(colIndex);
        CellValue cellValue = evaluator.evaluate(cell);
        if(cellValue!=null){
            int cellType = cellValue.getCellType();
            if (cellType==Cell.CELL_TYPE_STRING) {
               return cellValue.getStringValue();
            }
        }
        return null;
    }


    public static void CompareLoginName(){
        Map<String,String> dataSource = MysqlConnection.getRes();//数据库的 dispalyName loginName
        Map<String,String> excelSource  = getPinYinName();
        Set<String> excelKeySet = excelSource.keySet();
        for(String key:excelKeySet){
            String excelLoginName = excelSource.get(key);
            String dataSourceLoginName = dataSource.get(key.trim());
            if(dataSourceLoginName==null){
                PrintUtil.println(key+":"+excelLoginName+":数据库中不存在");
            }else{
                if(!(excelLoginName.equals(dataSourceLoginName))){
                    PrintUtil.println(key+"excel 和数据库的loginName不一致 数据库是:"+dataSourceLoginName+"excel:"+excelLoginName);
                }
            }
        }
    }


}
